package com.example.demo.controller;

import com.example.demo.entity.Plan;
import com.example.demo.service.PlanService;
import com.example.demo.utils.R;
import com.example.demo.utils.ReSet;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class PlanController {

    @Resource
    private PlanService planService;

    /**
     * 查询进度表中章节记录
     * */
    @PostMapping("selplan")
    public R selplan(Integer uid) {
        List<Plan> list = planService.selplan(uid);
        if (list != null){
            return ReSet.Ok(list);
        }
        return ReSet.Err();
    }

    /**
     * 提交进度
     * */
    @PostMapping("inplan")
    public R inplan(Integer uid, Integer tpid, Integer cnode,Integer score,Integer tscore) {
        int i = planService.inplan(uid,tpid,cnode,score,tscore);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }

    /**
     * 查询NODE数据量，若两表count相等，则开放id=count
     * */
    @PostMapping("selpnode")
    public R selpnode(Integer cnode,Integer uid) {
        int i = planService.selpnode(cnode,uid);
        int j = planService.selcnode(cnode);
        if (i == j){
            return ReSet.Ok();
        }
        return ReSet.Err();
    }

    /**
     * 查询进度表中node最大值用以判断考试进度
     * */
    @PostMapping("selmnode")
    public R selmnode(Integer uid) {
        int i = planService.selmnode(uid);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }

    /**
     * 查询node最大值对应的chapter_id
     * */
    @PostMapping("selmnodecpid")
    public R selmnodecpid(Integer cnode) {
        int i = planService.selmnodecpid(cnode);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }

    /**
     * 开启考试
     * */
    @PostMapping("uplan")
    public R uplan(Integer uid) {
        int i = planService.uplan(uid);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }

    /**
     * 进入下一章节,添加临时进度数据
     * */
    @PostMapping("incnodemax")
    public R incnodemax(Integer uid, Integer cnode) {

        List list = planService.selstandard(uid,cnode);
        if(list == null || list.size() == 0){
            int i = planService.incnodemax(uid,cnode);
            return ReSet.Ok(i);
        }else {
            return ReSet.Err(list);
        }
    }

    /**
     * 查询是否存在临时进度数据
     * 存在则删除，不存在则不反应
     * */
    @PostMapping("sellsdata")
    public R sellsdata(Integer uid, Integer cnode) {
        List<Plan> list = planService.sellsdata(uid,cnode);
        System.out.println(list);
        if (list == null || list.size() == 0){
            return ReSet.Err("暂无临时数据");
        }
        int i = planService.delucnode(uid,cnode);
        if (i > 0){
            return ReSet.Ok("删除成功");
        }
        return ReSet.Ok(list);
    }

    /**
     * 查询成绩
     * */
    @PostMapping("selscore")
    public R selscore(Integer cid,Integer uid){
        Plan plan = planService.selscore(cid, uid);
        if (plan != null){
            List list = new ArrayList();
            list.add(plan.getScore());
            list.add(plan.getTotalScore());
            return ReSet.Ok(list);
        }
        return ReSet.Err();

    }
    /**
     * 查询排行
     */
    @PostMapping("selranking")
    public R selranking(Integer tid,Integer uid) {
        List lists = new ArrayList();
        List list1 = planService.selranking(tid);//当前用户当前章节排名
        for(int i = 0;i < list1.size();i++){
            Map listu = (HashMap)list1.get(i);
            int uids = (int)listu.get("user_id");
            if (uids == uid){
                lists.add(listu);
            }
        }

        List list2 = planService.selcount(tid);//分数对应的人数
        List list3 = planService.selminscore(tid);//最低分
        List list4 = planService.selmaxscore(tid);//最高分

        lists.add(list2);
        lists.add(list3);
        lists.add(list4);

        return ReSet.Ok(lists);
    }

    /**
     * 试卷重做
     */
    @PostMapping("restart")
    public R restart(Integer uid,Integer tpid){
        int i = planService.delplan(uid, tpid);
        int j = planService.delrec(uid, tpid);
        if (i > 0 && j > 0){
            return ReSet.Ok("试卷已重置");
        }
        return ReSet.Err("重置失败");
    }


}
